package com.cspm.ahjy.bc.office.word.util;

import org.dom4j.Attribute;
import org.dom4j.Element;

import com.cspm.ahjy.bc.office.enums.PaperSizeEnum;
import com.cspm.ahjy.bc.office.word.domain.MSPaperSize;

/**
 * 页面宽高设置工具
 * @author ServerZhang
 * @date 2017年9月30日
 */
public class PageSizeUtil {

	/**
	 * 解析<w:pgSz>标签获取页面尺寸对象
	 * @param pgSzElement
	 * @return
	 * @author ServerZhang
	 * @date 2017年9月30日
	 */
	public static MSPaperSize getMsPaperSize(Element pgSzElement){
		if (pgSzElement == null) {
			return null;
		}
		Attribute wAttribute = pgSzElement.attribute("w");
		Attribute hAttribute = pgSzElement.attribute("h");
		if (wAttribute == null && hAttribute == null) {
			return null;
		}
		MSPaperSize msPaperSize = new MSPaperSize();
		
		Integer width = Integer.parseInt(wAttribute.getText());
		Integer height = Integer.parseInt(hAttribute.getText());
		msPaperSize.setWidth(width);
		msPaperSize.setHeight(height);
		
		String paperName = "自定义";
		PaperSizeEnum bySize = PaperSizeEnum.getBySize(width, height);
		if (bySize != null) {
			paperName = bySize.getPaperName();
		}
		msPaperSize.setSizeName(paperName);
		
		return msPaperSize;
	}
}
